let _Vue;

class Store {
    constructor(options) {
        const { state = {}, getters = {}, mutations = {}, actions = {} } = options;
        this.state = _Vue.observable(state)
        this.getters = Object.create(null)
        Object.keys(getters).forEach(key => {
            Object.defineProperty(this.getters, key, {
                get() {
                    return getters[key](state)
                }
            })
        })
        this._mutations = mutations
        this._actions = actions
    }
    commit(type, paylod) {
        this._mutations[type](this.state, paylod)
    }
    dispatch(type, paylod) {
        this._actions[type](this, paylod)
    }
}

function install(Vue) {
    _Vue = Vue;

    _Vue.mixin({
        beforeCreate(){
            if (this.$options.store) {
                _Vue.prototype.$store = this.$options.store
            }
        },
    })
}

export default {
    Store,
    install,
};
